﻿using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace Ycjw.PipeLine.Commons
{
    /// <summary>
    /// 扩展静态类
    /// </summary>
    public static class ExtendedUtils
    {
        private static string encryptKey = Guid.NewGuid().ToString("").Substring(0, 4);
        private static string encryptValue = "sxzy";

        public static string Encrypt(string str, string keyStr = "")
        {
            try
            {
                DESCryptoServiceProvider descsp = new DESCryptoServiceProvider();
                if (string.IsNullOrEmpty(keyStr))
                {
                    keyStr = encryptKey;
                }

                byte[] key = Encoding.Unicode.GetBytes(keyStr);
                byte[] data = Encoding.Unicode.GetBytes(str);
                MemoryStream MStream = new MemoryStream();
                CryptoStream CStream = new CryptoStream(MStream, descsp.CreateEncryptor(key, key), CryptoStreamMode.Write);
                CStream.Write(data, 0, data.Length);
                CStream.FlushFinalBlock();
                return Convert.ToBase64String(MStream.ToArray()) + keyStr;
            }
            catch (Exception ex)
            {
                Commons.LogHelper.Error(ex);
            }
            return string.Empty;
        }

        //字符串解密
        public static string Decrypt(string str, string keyStr = "")
        {
            DESCryptoServiceProvider descsp = new DESCryptoServiceProvider();
            try
            {
                if (string.IsNullOrEmpty(keyStr))
                {
                    keyStr = encryptKey;
                }
                byte[] key = Encoding.Unicode.GetBytes(keyStr);
                byte[] data = Convert.FromBase64String(str);
                MemoryStream MStream = new MemoryStream();
                CryptoStream CStram = new CryptoStream(MStream, descsp.CreateDecryptor(key, key), CryptoStreamMode.Write);
                CStram.Write(data, 0, data.Length);
                CStram.FlushFinalBlock();
                return Encoding.Unicode.GetString(MStream.ToArray());
            }
            catch (Exception ex)
            {
                Commons.LogHelper.Error(ex);
            }
            return string.Empty;
        }

        public static string Encrypts(string str)
        {
            try
            {
                DESCryptoServiceProvider descsp = new DESCryptoServiceProvider();
                byte[] key = Encoding.Unicode.GetBytes(encryptValue);
                byte[] data = Encoding.Unicode.GetBytes(str);
                MemoryStream MStream = new MemoryStream();
                CryptoStream CStream = new CryptoStream(MStream, descsp.CreateEncryptor(key, key), CryptoStreamMode.Write);
                CStream.Write(data, 0, data.Length);
                CStream.FlushFinalBlock();
                return Convert.ToBase64String(MStream.ToArray());
            }
            catch (Exception ex)
            {
                Commons.LogHelper.Error(ex);
            }
            return string.Empty;
        }

        //字符串解密
        public static string Decrypts(string str)
        {
            DESCryptoServiceProvider descsp = new DESCryptoServiceProvider();
            try
            {
                byte[] key = Encoding.Unicode.GetBytes(encryptValue);
                byte[] data = Convert.FromBase64String(str);
                MemoryStream MStream = new MemoryStream();
                CryptoStream CStram = new CryptoStream(MStream, descsp.CreateDecryptor(key, key), CryptoStreamMode.Write);
                CStram.Write(data, 0, data.Length);
                CStram.FlushFinalBlock();
                return Encoding.Unicode.GetString(MStream.ToArray());
            }
            catch (Exception ex)
            {
                Commons.LogHelper.Error(ex);
            }
            return string.Empty;
        }
    }
}